package com.example.week08.service.impl;

import com.example.week08.dao.mapper.OrderMapper;
import com.example.week08.dao.model.Order;
import com.example.week08.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @author 西柚
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 创建订单
     *
     * @param order
     */
    @Override
    public void createOrder(Order order) {
        orderMapper.insert(order);
    }

    /**
     * 批量创建订单
     */
    @Override
    public void batchCreateOrder() {
        long time = System.currentTimeMillis();

        ExecutorService executorService = Executors.newFixedThreadPool(100);

        List<Future<String>> list = new ArrayList<Future<String>>();
        for (int i = 1; i <= 10; i++) {
            list.add(executorService.submit(new Callable<String>() {
                        public String call() throws Exception {
                            orderMapper.batchInsert(getOrderList());
                            return null;
                        }
                    })
            );

        }
        for (Future<String> future : list) {
            try {
                future.get(5, TimeUnit.SECONDS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        executorService.shutdown();

        System.out.println("总耗时===" + (System.currentTimeMillis() - time) + "ms");
    }

    private List<Order> getOrderList() {
        List<Order> orderList = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            orderList.add(getOrder());
        }
        return orderList;
    }

    private Order getOrder() {
        Order order = new Order();
        Random random = new Random();
//        order.setOrderCode(IdGenerateUtil.genId() + random.nextInt());
        order.setUserId(888211438397689021L);
        order.setProductId(21312312312L);
        order.setOriginalPrice(new BigDecimal(100));
        order.setDiscountsPrice(new BigDecimal(10));
        order.setPayPrice(new BigDecimal(90));
        order.setReceiveName("李四");
        order.setReceivePhone("17777777778");
        order.setReceiveAddress("广东省广州市天河区XX路XX号");
        order.setPayType("wechat_app");
        order.setSource(1);
        order.setOrderStatus(1);
        order.setCreateBy("西柚");
        order.setCreateDate(new Date());
        order.setDelFlag(0);
        return order;
    }

    /**
     * 订单详情
     *
     * @param orderCode
     * @return
     */
    @Override
    public Order getOrderDetails(Long orderCode) {
        return orderMapper.selectById(orderCode);
    }

    /**
     * 根据用户ID查询订单列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Order> getByUserId(Long userId) {
        return orderMapper.getByUserId(userId);
    }

    public static void main(String[] args) {
        for (int k = 0; k < 4; k++) {
            // 新的4个库
            for (int h = 0; h < 64; h++) {
                // 新的64张表
                System.out.println("insert into training_camp_" + k + ".tb_order_" + h + " SELECT * FROM training_camp.tb_order where user_id % 4 = " + k + " and order_code % 64 = " + h + ";\n");
            }
            System.out.println("\n\n\n\n\n\n\n\n");
        }
    }
}
